//
//  DatePick.swift
//  ClockIn
//
//  Created by enrique leo on 15/8/21.
//

import SwiftUI

let date = "\(Date())"
    .split(separator: " ")[1]

struct PostView: View {
    @State private var title: String = "请输入标题（4-40个字）"
    @State private var titleFontColor: UIColor = UIColor.systemGray
    @State private var firstEdit1: Bool = true
    @State private var selfSectionRows: [SectionRow] = SectionRows
    
    var body: some View {
        //        DatePicker("Please enter a date", selection: $wakeUp, displayedComponents: .date)
        //            .background(Color.green)
        //            .labelsHidden()
        //            .datePickerStyle(CompactDatePickerStyle())
        NavigationView{
            ZStack{
                VStack{
                    // 标题框
                    TextField("", text: $title)
                        .onChange(of: title, perform: { value in
                            if firstEdit1 {
                                title = ""
                                firstEdit1 = false
                                titleFontColor = UIColor.black
                            }
                        })
                        .font(.title2)
                        .padding(EdgeInsets(top: 0, leading:  10, bottom: 10, trailing: 0))
                        .foregroundColor(Color(titleFontColor))
                    
                    Divider()
                    
                    Spacer()
                    
                    // 段落文本
                    ScrollView(showsIndicators: false) {
                        ForEach(selfSectionRows){ selfSectionRow in
                            SectionRowView(
                                sectionRow: selfSectionRow,
                                selfSectionRows: $selfSectionRows)
                                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.27, alignment: .center)
                        }
                    }
                    
                    Spacer()
                    
                    HStack{
                        // 添加段落按钮
                        addSectionButton
                            .padding(EdgeInsets(top: 0, leading:  5, bottom: 0, trailing: 0))
                        
                        Spacer()
                        
                        Text("@")
                            .padding(EdgeInsets(top: 0, leading:  0, bottom: 0, trailing: 5))
                    }
                    .frame(width: UIScreen.main.bounds.width, height: 10, alignment: .center)
                    .padding()
                    
                }
                .offset(y:-20)
            }
            .navigationBarItems(trailing: publishButton)
            .toolbar{
                ToolbarItem(placement: .principal) {
                    postTitle
                }
            }
        }
    }
}

extension PostView{
    private var postTitle: some View{
        VStack{
            Text("发帖子")
                .font(Font.system(size: 20))
            
            Text(date + " 已自动保存")
                .foregroundColor(Color(UIColor.separator))
                .font(Font.system(size: 12))
        }
    }
    
    private var publishButton: some View{
        HStack{
            Button(action:{
                selfSectionRows = SectionRows
                print(selfSectionRows)
            }){
                Text("发布")
            }
        }
    }
    
    private var addSectionButton: some View{
        HStack{
            Button(action: {
                selfSectionRows.append(SectionRow(content: "从这里开始分享...", id: selfSectionRows.count))
                SectionRows = selfSectionRows
                print(selfSectionRows.count)
            }) {
                HStack{
                    ZStack{
                        Circle()
                            .strokeBorder(Color(UIColor.systemGray), lineWidth: 2)
                            .frame(width: 30, height: 30)
                            .offset(x: -0.5, y: 1)
                        
                        HStack{
                            Text("+")
                                .font(Font.system(size: 30))
                                .foregroundColor(Color(UIColor.systemGray))
                            
                        }
                    }
                    Text("添加段落")
                }
            }
        }
    }
}


struct DatePick_Previews: PreviewProvider {
    static var previews: some View {
        PostView()
    }
}
